/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "AppDelegate.h"
#import "EntryEntryAbilityViewController.h"
//#import "Oho_api_applicationOho_api_applicationAbilityViewController.h"
//#import "EntryFragmentAbilityOneViewController.h"
//#import "EntryFragmentAbilityTwoViewController.h"
//#import "ApimoduleApimoduleAbilityViewController.h"
#import <libarkui_ios/StageApplication.h>
#import "BaseModuleViewController.h"
#import "APITestViewController.h"
#import "DynamicDownLoadViewController.h"
#import "OneToMoreViewController.h"
#import "BridgeTestNativeViewController.h"
#import "NativeViewController.h"

#define BUNDLE_DIRECTORY @"arkui-x"
//#define BUNDLE_NAME @"com.example.enjoyarkuix_ios"
#define BUNDLE_NAME @"com.example.enjoyarkuix"


@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [StageApplication configModuleWithBundleDirectory:BUNDLE_DIRECTORY];
    [StageApplication launchApplication];
    
    NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",BUNDLE_NAME, @"entry", @"EntryAbility"];
    EntryEntryAbilityViewController *mainView = [[EntryEntryAbilityViewController alloc] initWithInstanceName:instanceName];
    [self setNavRootVC:mainView];
    return YES;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
    NSLog(@"appdelegate openUrl callback, url : %@", url.absoluteString); // eg: (com.entry.arkui://entry?OtherAbility)
    
    NSString *bundleName = url.scheme;
    NSString *moduleName = url.host;
    NSString *abilityName, *params;

    NSURLComponents * urlComponents = [NSURLComponents componentsWithString:url.absoluteString];
    NSArray <NSURLQueryItem *> *array = urlComponents.queryItems;
    for (NSURLQueryItem * item in array) {
        if ([item.name isEqualToString:@"abilityName"]) {
            abilityName = item.value;
        } else if ([item.name isEqualToString:@"params"]) {
            params = item.value;
        }
    }

    [self handleOpenUrlWithBundleName:bundleName
                           moduleName:moduleName
                          abilityName:abilityName
                               params:params, nil];
    
    return YES;
}

- (BOOL)handleOpenUrlWithBundleName:(NSString *)bundleName
                         moduleName:(NSString *)moduleName
                        abilityName:(NSString *)abilityName
                             params:(NSString *)params, ...NS_REQUIRES_NIL_TERMINATION {
    
    id rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    BOOL hasRoot = NO;
    if ([rootVC isKindOfClass:[UINavigationController class]]) {
        hasRoot = YES;
    }
    
    id subStageVC = nil;
    
    if ([params containsString:@"basemodule"]) {
        BaseModuleViewController *baseModuleVC = [[BaseModuleViewController alloc] init];
        baseModuleVC.isShowNavigationBar = YES;
        baseModuleVC.titleName = @"基础组件";
        baseModuleVC.instanceName = [NSString stringWithFormat:@"%@:%@:%@", BUNDLE_NAME, @"basemodule", @"BaseModuleAbility"];
        baseModuleVC.hidesBottomBarWhenPushed = YES;
        [rootVC pushViewController:baseModuleVC animated:YES];
    } else if ([params containsString:@"oho_api_application"]) {
        NSString *apiInstanceName = [NSString stringWithFormat:@"%@:%@:%@", BUNDLE_NAME, @"oho_api_application", @"Oho_api_applicationAbility"];
//        NativeViewController *nativeVC = [[NativeViewController alloc] init];
//        nativeVC.instanceName = apiInstanceName;
        APITestViewController *apiVC = [[APITestViewController alloc] initWithInstanceName:apiInstanceName];
        apiVC.titleName = @"子系统API";
        apiVC.hidesBottomBarWhenPushed = YES;
      
        [rootVC pushViewController:apiVC animated:YES];
    } else if ([moduleName containsString:@"dynamization"]) {
        DynamicDownLoadViewController *baseModuleVC = [[DynamicDownLoadViewController alloc] init];
        baseModuleVC.titleName = @"动态化";
        baseModuleVC.hidesBottomBarWhenPushed = YES;
        [rootVC pushViewController:baseModuleVC animated:YES];
    } else if ([moduleName containsString:@"show"]) {
        OneToMoreViewController *oneToMoreVC = [[OneToMoreViewController alloc] init];
        oneToMoreVC.titleName = @"C跨平台";
        oneToMoreVC.pageType = Page_Type_C;
        oneToMoreVC.hidesBottomBarWhenPushed = YES;
        [rootVC pushViewController:oneToMoreVC animated:YES];
    } else if ([moduleName containsString:@"bridge"]) {
        BridgeTestNativeViewController* bridgeTestNativeVC = [[BridgeTestNativeViewController alloc] init];
        bridgeTestNativeVC.titleName = @"bridge桥接原生接口";
        bridgeTestNativeVC.hidesBottomBarWhenPushed = YES;
        [rootVC pushViewController:bridgeTestNativeVC animated:YES];
    }
    
    
    return YES;
    
    
    NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",bundleName, moduleName, abilityName];
    EntryEntryAbilityViewController *entryOtherVC = [[EntryEntryAbilityViewController alloc] initWithInstanceName:instanceName];
    entryOtherVC.params = params;
    subStageVC = (EntryEntryAbilityViewController *)entryOtherVC;
    
    if (!subStageVC) {
        return NO;
    }
    
    if (!hasRoot) {
        [self setNavRootVC:subStageVC];
    } else {
        UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
        [rootNav pushViewController:subStageVC animated:YES];
    }
    return YES;
}

- (void)setNavRootVC:(id)viewController {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    UINavigationController *rootNavigationVC = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = rootNavigationVC;
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
//    if (_allowRotation) {
//        return UIInterfaceOrientationMaskAll;
//    }
//    return UIInterfaceOrientationMaskPortrait;
}

@end
